package com.jessechanrui.template.core.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author chenrui
 * @version 1.0
 * @description DES3 加解密工具类
 * @date 2025/8/8
 */
public class DES3Util {

    private static final Base64.Encoder encoder = Base64.getEncoder();

    private static final Base64.Decoder decoder = Base64.getDecoder();

    /**
     * 密钥(DES加密和解密过程中，密钥长度都必须是8的倍数)
     */
    private final static String SECRET_KEY = "ASC0123456789ZYXWVUTSLIU";

    /**
     * 加解密统一使用的编码方式
     */
    private final static String ENCODING = StandardCharsets.UTF_8.name();

    /**
     * DES3 加密
     *
     * @param plainText 普通文本
     */
    public static String encode(String plainText) throws Exception {
        byte[] bytes = DesUtils.encrypt3(plainText.getBytes(ENCODING), SECRET_KEY.getBytes());
        byte[] encode = encoder.encode(bytes);
        return new String(encode, ENCODING);
    }

    /**
     * DES3 解密
     *
     * @param encryptText 加密文本
     */
    public static String decode(String encryptText) throws Exception {
        byte[] decode = decoder.decode(encryptText);
        byte[] bytes = DesUtils.decrypt3(decode, SECRET_KEY.getBytes(ENCODING));
        return new String(bytes, ENCODING);
    }

}
